<template>
  <div class="prayeradd">
    <b-container>
      <img class="img-fluid mb-3" src="http://www.xinde.org/uploadfile/other/prayer/me.jpg" style="width:100%">
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px">
        <el-form-item label="祈祷者：" prop="name">
          <el-input v-model="ruleForm.name"></el-input>
        </el-form-item>
        <el-form-item label="祈祷意向：" prop="type_id">
          <el-select v-model="ruleForm.type_id" placeholder="请选择祈祷意向">
            <el-option v-for="item in types" :key="item.id" :label="item.title" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="献花：" prop="hua_id">
          <el-radio-group v-model="ruleForm.hua_id">
            <el-radio v-for="item in flower" :key="item.id" :label="item">
              <img :src="'/uploadfile/other/prayer/' + item + '.jpg'" style="max-height: 60px">
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="祈祷内容：" prop="content">
          <el-input type="textarea" v-model="ruleForm.content" maxlength="800" rows="8"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </b-container>
    <Footer msg="Welcome to Your Vue.js App" />
  </div>
</template>

<script>
import Vue from "vue";
import Footer from "@/components/Footer.vue";
import { mapState, mapActions } from "vuex";
import {
  Input,
  InputNumber,
  Radio,
  RadioGroup,
  Button,
  Select,
  Option,
  Form,
  FormItem,
  Message
} from "element-ui";
Vue.use(Input);
Vue.use(InputNumber);
Vue.use(Radio);
Vue.use(RadioGroup);
Vue.use(Button);
Vue.use(Select);
Vue.use(Option);
Vue.use(Form);
Vue.use(FormItem);


Vue.prototype.$message = Message;
export default {
  name: "prayeradd",
  components: {
    Footer
  },
  data () {
    return {
      ruleForm: {
        name: "",
        hua_id: 1,
        type_id: 88,
        content: ""
      },
      flower: [1, 3, 6, 7, 8, 10, 11, 17, 18, 19],
      rules: {
        name: [
          //{ required: true, message: '请输入活动名称', trigger: 'blur' },
          { max: 8, message: "祈祷者不能超过8个字符", trigger: "blur" }
        ],
        content: [
          { required: true, message: "请填写祈祷内容", trigger: "blur" }
        ]
      },
      types: []
    };
  },
  created () {
    this.setList();
  },
  computed: {
    ...mapState({ typeList: state => state.type.list })
  },
  watch: {
    typeList: function (val) {
      this.types = this.common.getTypes(val, "xdprayer");
    }
  },
  methods: {
    submitForm (formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          if (!this.ruleForm.name) this.ruleForm.name = "佚名";
          if (!this.ruleForm.hua_id) this.ruleForm.hua_id = 1;
          let url = process.env.VUE_APP_prayerAdd_url;
          window.axios.post(url, this.ruleForm).then(ret => {
            let _this = this;
            this.$message({              message: '祈祷成功', onClose: function () {
                _this.$router.push('/prayer')
              }            });

          });
        } else {
          return false;
        }
      });
    },
    resetForm (formName) {
      this.$refs[formName].resetFields();
    },
    ...mapActions("type", ["setList"])
  }
};
</script>